package one.stand.service.impl;

import one.stand.mapper.GoodsPhotoMapperExt;
import one.stand.model.GoodsPhoto;
import one.stand.service.GoodsPhotoBaseService;
import one.stand.util.OneConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author cjr
 */
@Service
public class GoodsPhotoBaseServiceImpl implements GoodsPhotoBaseService {
    @Autowired
    private GoodsPhotoMapperExt goodsPhotoMapperExt;

    @Override
    @Transactional
    public void save(Integer goodsId, String photos) {
        // 删除以前商品图片
        goodsPhotoMapperExt.deleteByGoodsId(goodsId);
        // 重新添加商品图片
        if (StringUtils.isNoneBlank(photos)) {
            Date date = new Date();
            int n = 0;
            GoodsPhoto goodsModel;
            for (String photo : photos.split(OneConstant.arraySymbol)) {
                goodsModel = new GoodsPhoto();
                goodsModel.setGoodsId(goodsId);
                goodsModel.setUrl(photo);
                goodsModel.setSort(n++);
                goodsModel.setCreateTime(date);
                goodsModel.setIsDel(0);
                goodsPhotoMapperExt.insertSelective(goodsModel);
            }
        }
    }
}
